package action.UserAction;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import model.dao.LoginDao;

import com.opensymphony.xwork2.ActionSupport;

import domain.PetInfo;
import domain.UserInfo;

public class LoginAction extends ActionSupport {
	UserInfo user = new UserInfo();

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

	public String login() throws Exception {
		LoginDao logindao = new LoginDao();
		boolean result = logindao.search(user.getUserName(), user.getUserPwd());
		if (result == true) {
			UserInfo User_bak=new UserInfo();
			User_bak.setUserId(logindao.getUser_Id());
			User_bak.setUserName(logindao.getUser_Name());
			User_bak.setUserPwd(logindao.getUser_Pwd());
			User_bak.setUserSex(logindao.getUser_Sex());
			User_bak.setUserPlace(logindao.getUser_Place());
			User_bak.setUserTel(logindao.getUser_Tel());
			User_bak.setUserRealName(logindao.getUser_RealName());
			User_bak.setUserAge(logindao.getUser_Age());
			User_bak.setUserEmail(logindao.getUser_Email());
			HttpSession session=ServletActionContext.getRequest().getSession();
			session.setAttribute("UserLogin", User_bak);
			ArrayList<PetInfo> BuyPets=new ArrayList<PetInfo>();
			HashMap<UserInfo,ArrayList<PetInfo>> BuyCarSession=new HashMap<UserInfo,ArrayList<PetInfo>>();
			BuyCarSession.put(User_bak, BuyPets);
			session.setAttribute("BuyCar", BuyCarSession);
			return SUCCESS;
		}
		else
			return "loginfalse";
	}
	public String RemoveUser() throws Exception {
		HttpSession session=ServletActionContext.getRequest().getSession();
		session.removeAttribute("UserLogin");
		return "RemoveSuccess";
	}

}
